﻿using System;
using System.Collections.Generic;
using System.Text;

using GDModel.Model;

namespace BLL
{
    public class Battle
    {
        private static Dictionary<int, Dictionary<Charactor, Monster>> _battle = null;

        static Battle()
        {
            _battle = new Dictionary<int, Dictionary<Charactor, Monster>>();
            Cock _cock = new Cock(20080317,20, 5, 5);
            Charactor _cha = new Charactor(18890420,30, 5, 5);

            Dictionary<Charactor, Monster> _battleList = new Dictionary<Charactor, Monster>();

            _battleList.Add(_cha, _cock);

            _battle.Add(18890420, _battleList);
        }

        public static void OneInOne(int _cha, int _mon, out Health health1, out Health health2)
        {
            Dictionary<Charactor, Monster> _battleList = _battle[_cha];

            if (_battleList.Keys.Count < 1) {

                health1 = null;
                health2 = null;
                return; }

            Monster mon = null;
            Charactor cha = null;

            foreach (KeyValuePair<Charactor, Monster> entry in _battleList)
            {
                mon = entry.Value;
                cha = entry.Key;
                break;
            }

            if (mon==null || cha==null)
            {
                health1 = null;
                health2 = null;
                return;
            }

            if (mon.Health<=0 || cha.Health<=0)
            {
                health1 = null;
                health2 = null;
                return;
            }


            if (mon.Id != _mon) {
                health1 = null;
                health2 = null;
                return; }

            mon.Health -= cha.Attack;
            mon.Hurt = cha.Attack;
            health1 = new Health();
            health2 = new Health();
            health1.Hurt = mon.Hurt;
            health1.Remain = mon.Health;
            if (mon.Health < 0)
            {
                mon.Health = 0;
                health2.Hurt = 0;
                health2.Remain = cha.Health;
            }
            else
            {
                cha.Health -= mon.Attack;
                cha.Hurt = mon.Attack;
                health2.Hurt = cha.Hurt;
                health2.Remain = cha.Health;
            }

            //if (mon.Health <= 0 || cha.Health <= 0)
            //    _battleList.Clear();
        }

        public static void GetItem(int _cha, int _mon, out Capture _capture)
        {
            Dictionary<Charactor, Monster> _battleList = _battle[_cha];

            if (_battleList.Keys.Count < 1)
            {

                _capture = null;
                return;
            }

            Monster mon = null;
            Charactor cha = null;

            foreach (KeyValuePair<Charactor, Monster> entry in _battleList)
            {
                mon = entry.Value;
                cha = entry.Key;
                break;
            }

            if (mon == null || cha == null)
            {
                _capture = null;
                return;
            }

            if ((mon.Health >0 && cha.Health > 0)||(cha.Health<=0))
            {
                _capture = null;
                return;
            }
            if (mon.Id != _mon)
            {
                _capture = null;
                return;
            }

            _capture = new Capture();
            Item item = new Item();
            item.Name = "鸡肉";
            _capture.IItem = item;

            _battleList.Clear();
        }

 
    }
}
